from datetime import datetime, date, timedelta
import calendar

# 计算当前月份的日期范围
def get_month_range(start_date = None):
    if start_date is None:
        # replace函数将days属性设置成1,获取对应月份第一天的日期
        start_date = date.today().replace(day=1)
    # calendar.monthrange()函数来找出该月的总天数
    _, days_in_month = calendar.monthrange(start_date.year, start_date.month)
    end_date = start_date + timedelta(days=days_in_month)
    return (start_date, end_date)

def demo_1():
    a_day = timedelta(days=1)
    first_day, last_day = get_month_range()
    while first_day < last_day:
        print(first_day)
        first_day += a_day

def date_range(start, stop, step):
    while start < stop:
        yield start
        start += step

# 使用生成器获取时间范围
def demo_2():
    for d in date_range(datetime(2012, 9, 1), datetime(2012, 10, 1), timedelta(hours=6)):
        print(d)

if __name__ == '__main__':
    # demo_1()
    demo_2()